package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.bean.request.QueryPropertyConfigRequest;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.BasePropertyConfig;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class BasePropertyConfigDAO extends HibernateSupportDelegateDAO<BasePropertyConfig> {
    public BasePropertyConfigDAO() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = "from BasePropertyConfig where objectId=:objectId")
    public abstract List<BasePropertyConfig> queryAllPropertyConfig(@DAOParam("objectId") String objectId);

    @DAOMethod(sql = "from BasePropertyConfig where propertyId=:propertyId")
    public abstract BasePropertyConfig getPropertyConfigByModuleId(@DAOParam("propertyId") Integer propertyId);

    @DAOMethod(sql = "from BasePropertyConfig where objectId=:objectId and moduleId=:moduleId")
    public abstract List<BasePropertyConfig> queryPropertyConfigByModuleId(@DAOParam("objectId") String objectId,@DAOParam("moduleId") String moduleId);

    @Deprecated
    @DAOMethod(sql = "from BasePropertyConfig where propertyCode=:propertyCode and objectId=:objectId")
    public abstract BasePropertyConfig getPropertyConfigByPropertyCode(@DAOParam("propertyCode") String propertyCode, @DAOParam("objectId") String objectId);


    public List<BasePropertyConfig> queryPropertyConfig(final String objectId, final String moduleId, final String propertyCode) {
        HibernateStatelessResultAction<List<BasePropertyConfig>> action = new AbstractHibernateStatelessResultAction<List<BasePropertyConfig>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hql = new StringBuffer();
                hql.append(" SELECT id,moduleId,openServiceId,moduleName,propertyCode,propertyName,explainData, ");
                hql.append(" propertyLevel,objectId,objectName ,");
                hql.append(" propertyData,unit,propertyId  ");
                hql.append(" FROM BasePropertyConfig  ");
                hql.append(" where 1=1 ");

                if (StringUtils.isNotEmpty(objectId)){
                    hql.append(" and objectId=:objectId ");
                }
                if (StringUtils.isNotEmpty(moduleId)) {
                    hql.append("and moduleId=:moduleId ");
                }
                if (StringUtils.isNotEmpty(propertyCode)) {
                    hql.append("and propertyCode=:propertyCode ");
                }
                Query query = ss.createQuery(hql.toString());
                if (StringUtils.isNotEmpty(objectId)){
                    query.setParameter("objectId", objectId);
                }
                if (StringUtils.isNotEmpty(moduleId)) {
                    query.setParameter("moduleId", moduleId);
                }
                if (StringUtils.isNotEmpty(propertyCode)) {
                    query.setParameter("propertyCode", propertyCode);
                }
//                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<BasePropertyConfig> basePropertyConfig = query.list();
//                List<Map> list = query.list();
//                if (list.size() > 0) {
//                    for (Map map : list) {
//                        basePropertyConfig.add(ConversionUtils.convert(map, BasePropertyConfig.class));
//                    }
//                }
                setResult(basePropertyConfig);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<BasePropertyConfig> queryPropertyConfigByFuzzy(QueryPropertyConfigRequest request) {
        HibernateStatelessResultAction<List<BasePropertyConfig>> action = new AbstractHibernateStatelessResultAction<List<BasePropertyConfig>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hql = new StringBuffer();
                hql.append(" SELECT id,module_id,open_service_id,module_name,property_code,property_name,explain_data, ");
                hql.append(" property_level,object_id,object_name ,");
                hql.append(" property_data,unit,property_id  ");
                hql.append(" FROM base_property_config  ");
                hql.append(" where 1=1 ");

                if (StringUtils.isNotEmpty(request.getObjectId())){
                    hql.append(" and object_id=:objectId ");
                }
                if (StringUtils.isNotEmpty(request.getModuleId())) {
                    hql.append("and module_id=:moduleId ");
                }
                if (StringUtils.isNotEmpty(request.getFuzzyCondition())) {
                    hql.append(" and (explain_data like (:fuzzyCondition)  or property_name like (:fuzzyCondition))  ");
                    request.setFuzzyCondition("%"+request.getFuzzyCondition()+"%");
                }
                Query query = ss.createSQLQuery(hql.toString());
                query.setProperties(request);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<BasePropertyConfig> basePropertyConfig = new ArrayList<>();
                List<Map> list = query.list();
                if (list.size() > 0) {
                    for (Map map : list) {
                        basePropertyConfig.add(ConversionUtils.convert(map, BasePropertyConfig.class));
                    }
                }
                setResult(basePropertyConfig);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public List<BasePropertyConfig> queryPropertyConfigList(final String objectId, final List<String> propertyCodes) {
        HibernateStatelessResultAction<List<BasePropertyConfig>> action = new AbstractHibernateStatelessResultAction<List<BasePropertyConfig>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hql = new StringBuffer();
                hql.append("from BasePropertyConfig where 1=1 ");

                if (CommonUtils.isNotEmpty(objectId)) {
                    hql.append("and objectId=:objectId ");
                }
                if (CommonUtils.isNotEmpty(propertyCodes)) {
                    hql.append("and propertyCode in(:propertyCodes) ");
                }
                Query query = ss.createSQLQuery(hql.toString());
                if (CommonUtils.isNotEmpty(objectId)) {
                    query.setParameter("objectId", objectId);
                }
                if (CommonUtils.isNotEmpty(propertyCodes)) {
                    query.setParameterList("propertyCodes", propertyCodes);
                }
                //query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<BasePropertyConfig> basePropertyConfig = (List<BasePropertyConfig>)query.list();
//                List<Map> list = query.list();
//                if (list.size() > 0) {
//                    for (Map map : list) {
//                        basePropertyConfig.add(ConversionUtils.convert(map, BasePropertyConfig.class));
//                    }
//                }
                setResult(basePropertyConfig);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}